package fr.xebia.view;

import java.util.Collections;
import java.util.List;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

import fr.xebia.data.Adresse;
import fr.xebia.data.Client;
import fr.xebia.service.AdresseService;
import fr.xebia.service.ClientService;
import fr.xebia.service.ServiceException;

/**
 * 
 */
public class ClientAction extends GenericAction implements ModelDriven<Client> {

	private static final long serialVersionUID = 1L;

	private ClientService clientService;

	private Client client;

	public Client getClient() {
		return client;
	}

	/**
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	public Client getModel() {
		client = new Client();
		return client;
	}

	/**
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		super.prepare();
		logger.debug("Preparing " + getClass().getName());
		clientService = serviceLocator.getClientService();
		addAdresses();
	}

	/**
	 * @throws ServiceException
	 */
	protected void addAdresses() throws Exception {
		AdresseService adresseService = serviceLocator.getAdresseService();
		List<Adresse> adresses = adresseService.list();
		request.setAttribute("adresses", adresses);
	}

	public String prepareadd() throws Exception {
		return Action.SUCCESS;
	}

	public String list() throws Exception {
		try {
			List<Client> clients = clientService.list();
			request.setAttribute("clients", clients);
			logger.debug("Found " + clients.size() + " clients");
			return Action.SUCCESS;
		} catch (Exception e) {
			request.setAttribute("clients", Collections.EMPTY_LIST);
			return handleError("Can't get clients", e);
		}
	}

	public String search() throws Exception {
		try {
			List<Client> clients = clientService.search(client);
			request.setAttribute("clients", clients);
			logger.debug("Found " + clients.size() + " clients");
			return Action.SUCCESS;
		} catch (Exception e) {
			request.setAttribute("clients", Collections.EMPTY_LIST);
			return handleError("Can't get clients", e);
		}
	}

	public String add() throws Exception {
		try {
			logger.info("Adding new client");
			Long id = (Long) clientService.save(client);
			client.setId(id);
			addActionMessage("Added new client with id: " + client.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't add client", e);
		}
	}

	public String update() throws Exception {
		try {
			logger.info("Updating client with id: " + client.getId());
			clientService.update(client);
			addActionMessage("Updated client with id: " + client.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't save client", e);
		}
	}

	public String edit() throws Exception {
		try {
			logger.info("Getting client with id: " + client.getId());
			client = clientService.get(client.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't get client", e);
		}
	}

	public String delete() throws Exception {
		try {
			logger.info("Deleting client with id: " + client.getId());
			clientService.deleteById(client.getId());
			addActionMessage("Deleted client with id: " + client.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't delete client", e);
		}
	}
}
